package com.chenglai.article.service.impl;

import com.chenglai.article.dao.ArticleCategoryMapper;
import com.chenglai.article.dao.ArticleInfoMapper;
import com.chenglai.article.pojo.ArticleCategory;
import com.chenglai.article.pojo.ArticleInfo;
import com.chenglai.article.service.ArticleCategoryService;
import com.chenglai.commonutils.common.DateFormatHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.ibatis.annotations.Param;
import tk.mybatis.mapper.entity.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 类描述：
 *
 * @Author rxh
 * @Date 2023/11/15 11:41
 */
@Service
public class ArticleCategoryServiceImpl implements ArticleCategoryService {

    @Resource
    private ArticleCategoryMapper articleCategoryMapper;

    @Resource
    private ArticleInfoMapper articleInfoMapper;

    @Override
    public PageInfo getArticleCateGoryList(Integer page, Integer size, Integer shopId) {
        PageHelper.startPage(page, size);
        Example example = new Example(ArticleCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("shopId", shopId);
        example.orderBy("categoryId").desc();
        return new PageInfo(articleCategoryMapper.selectByExample(example));
    }

    @Override
    public List<ArticleCategory> getArticleCateGoryList(Integer shopId) {
        Example example = new Example(ArticleCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("shopId", shopId);
        example.orderBy("categoryId").desc();
        return articleCategoryMapper.selectByExample(example);
    }

    @Override
    public void editArticleCategory(ArticleCategory articleCategory) {
        if (articleCategory.getCategoryId() == null) {
            //添加
            articleCategory.setIsDelete(1);
            articleCategory.setCreateTime(DateFormatHelper.currentTimes());
            articleCategoryMapper.insertSelective(articleCategory);
        } else {
            //修改
            articleCategoryMapper.updateByPrimaryKeySelective(articleCategory);
        }
    }

    @Override
    public Map<String,Object> delArticleCategory(Integer categoryId) {
        Map<String,Object> data = new HashedMap<>();
        //判断该分类是否还有数据，如有 无法删除
        List<ArticleInfo> list = queryArticleListByCategoryId(categoryId);
        if (list.size() > 0) {
            data.put("flag","error");
            data.put("msg","该分类关联了文章，不可删除");
            return data;
        } else {
            articleCategoryMapper.deleteByPrimaryKey(categoryId);
            data.put("flag","success");
            data.put("msg","删除成功");
            return data;
        }
    }

    @Override
    public ArticleCategory categoryDetail(Integer categoryId) {
        return articleCategoryMapper.selectByPrimaryKey(categoryId);
    }

    @Override
    public List<ArticleInfo> queryArticleListByCategoryId(Integer categoryId) {
        Example example = new Example(ArticleInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("categoryId", categoryId);
        example.orderBy("articleSort").desc();
        return articleInfoMapper.selectByExample(example);
    }
}
